/**
 * 二维向量类 - 处理坐标、方向、运动计算
 */
class Vector2D {
    constructor(x = 0, y = 0) {
        this.x = x;
        this.y = y;
    }

    // 设置向量值
    set(x, y) {
        this.x = x;
        this.y = y;
        return this;
    }

    // 复制向量
    copy(vector) {
        this.x = vector.x;
        this.y = vector.y;
        return this;
    }

    // 克隆向量
    clone() {
        return new Vector2D(this.x, this.y);
    }

    // 向量加法
    add(vector) {
        this.x += vector.x;
        this.y += vector.y;
        return this;
    }

    // 向量减法
    subtract(vector) {
        this.x -= vector.x;
        this.y -= vector.y;
        return this;
    }

    // 标量乘法
    multiply(scalar) {
        this.x *= scalar;
        this.y *= scalar;
        return this;
    }

    // 标量除法
    divide(scalar) {
        if (scalar !== 0) {
            this.x /= scalar;
            this.y /= scalar;
        }
        return this;
    }

    // 计算向量长度
    magnitude() {
        return Math.sqrt(this.x * this.x + this.y * this.y);
    }

    // 计算向量长度的平方（性能优化）
    magnitudeSquared() {
        return this.x * this.x + this.y * this.y;
    }

    // 单位化向量
    normalize() {
        const mag = this.magnitude();
        if (mag > 0) {
            this.divide(mag);
        }
        return this;
    }

    // 限制向量长度
    limit(max) {
        const mag = this.magnitude();
        if (mag > max) {
            this.normalize().multiply(max);
        }
        return this;
    }

    // 计算两点距离
    distance(vector) {
        const dx = this.x - vector.x;
        const dy = this.y - vector.y;
        return Math.sqrt(dx * dx + dy * dy);
    }

    // 计算两点距离的平方
    distanceSquared(vector) {
        const dx = this.x - vector.x;
        const dy = this.y - vector.y;
        return dx * dx + dy * dy;
    }

    // 点积
    dot(vector) {
        return this.x * vector.x + this.y * vector.y;
    }

    // 计算角度（弧度）
    angle() {
        return Math.atan2(this.y, this.x);
    }

    // 从角度创建向量
    fromAngle(angle, magnitude = 1) {
        this.x = Math.cos(angle) * magnitude;
        this.y = Math.sin(angle) * magnitude;
        return this;
    }

    // 旋转向量
    rotate(angle) {
        const cos = Math.cos(angle);
        const sin = Math.sin(angle);
        const x = this.x * cos - this.y * sin;
        const y = this.x * sin + this.y * cos;
        this.x = x;
        this.y = y;
        return this;
    }

    // 线性插值
    lerp(vector, t) {
        this.x = this.x + (vector.x - this.x) * t;
        this.y = this.y + (vector.y - this.y) * t;
        return this;
    }

    // 检查是否为零向量
    isZero() {
        return this.x === 0 && this.y === 0;
    }

    // 转换为字符串
    toString() {
        return `Vector2D(${this.x.toFixed(2)}, ${this.y.toFixed(2)})`;
    }

    // 静态方法：创建随机向量
    static random(magnitude = 1) {
        const angle = Math.random() * Math.PI * 2;
        return new Vector2D().fromAngle(angle, magnitude);
    }

    // 静态方法：从两点创建向量
    static fromPoints(p1, p2) {
        return new Vector2D(p2.x - p1.x, p2.y - p1.y);
    }

    // 静态方法：向量加法
    static add(v1, v2) {
        return new Vector2D(v1.x + v2.x, v1.y + v2.y);
    }

    // 静态方法：向量减法
    static subtract(v1, v2) {
        return new Vector2D(v1.x - v2.x, v1.y - v2.y);
    }

    // 静态方法：向量乘法
    static multiply(vector, scalar) {
        return new Vector2D(vector.x * scalar, vector.y * scalar);
    }

    // 静态方法：线性插值
    static lerp(v1, v2, t) {
        return new Vector2D(
            v1.x + (v2.x - v1.x) * t,
            v1.y + (v2.y - v1.y) * t
        );
    }
} 